package com.e_library.dao.classes;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.e_library.jpa.entities.Main;
import com.e_library.jpa.entities.Theme;


public class ThemeDaoImpl implements ThemeIDao{

	private EntityManager em;
    public ThemeDaoImpl() {
    	Main m=new Main();
          em=m.em;
          em.getTransaction().begin();
	}
	 public void deleteOne(String id) {
		 Theme  rayon= em.find(Theme.class, id);
	 if (rayon == null) {
	throw new DaoException(2);
	}
	em.remove(rayon);
	 em.getTransaction().commit();
	 }
	
	@SuppressWarnings("unchecked")
  public List<Theme> getAll() {
	 return em.createQuery("select p from Theme p").getResultList();
	 }
	
	 @SuppressWarnings("unchecked")
	 public List<Theme> getAllLike(String modele) {
	 return em.createQuery("select p from Theme p where p.nom_theme like :modele")
	 .setParameter("modele", modele).getResultList();
	 }
	
	 public Theme getOne(String id) {
	 return em.find(Theme.class, id);
	 
	 }
	

	 public Theme saveOne(Theme theme) {
	 em.persist(theme);
	 em.getTransaction().commit();
	 return theme;
}
	
	 
	 public Theme updateOne(Theme theme) {
	Theme theme2=em.merge(theme);	 
		 em.getTransaction().commit();
	 return theme2;
	 }
}
